package com.horoscope.astrology.zodiac.signs.free.activity

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import com.horoscope.astrology.zodiac.signs.free.AppSettingManager
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.bean.UserBean
import com.horoscope.astrology.zodiac.signs.free.utils.EventType
import com.horoscope.astrology.zodiac.signs.free.utils.FlurryEventUtil
import kotlinx.android.synthetic.main.activity_name_compatibility.*

class NameCompatibilityActivity : BaseActivity() {


    private var mUserBean = UserBean()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        intent?.let {
            if (it.getBooleanExtra("Alarm", false)) {
                activityShowAd("Push_Name")
            }
        }
        setContentView(R.layout.activity_name_compatibility)
        initTitleBar(
            this,
            R.id.btn_back_edit_name,
            R.id.text_view_back_title,
            ""
        )
        mUserBean = AppSettingManager.readObject("user_info") ?: return
        edit_text_name_1.setText(mUserBean.name)
        showSoftInputFromWindow(this@NameCompatibilityActivity, edit_text_name_2)
        initListeners()
        FlurryEventUtil.putDataToEvent(EventType.TYPE_NAME_COMPATIBILITY_SHOW, null, null)
    }

    private fun initListeners() {
        edit_text_name_1.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                buttonIsEnable()
            }
        })
        edit_text_name_2.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                buttonIsEnable()
            }
        })
        text_view_get_result?.setOnClickListener {
            val name1 = edit_text_name_1.text.toString().trim()
            val name2 = edit_text_name_2.text.toString().trim()
            if (name1.isNotEmpty() && name2.isNotEmpty()) {
                try {
                    if (intent.getBooleanExtra("Alarm", false)) {   // 通过通知进入
                        startActivity(
                            NameResultActivity.generateIntent(
                                this,
                                name1,
                                name2,
                                intent.getBooleanExtra("Alarm", false),
                                intent.getStringExtra("workName")
                            )
                        )
                    } else {    // 正常进入
                        startActivity(NameResultActivity.generateIntent(this, name1, name2))
                    }
                } catch (e: Exception) {
                    startActivity(NameResultActivity.generateIntent(this, name1, name2))
                }
                FlurryEventUtil.putDataToEvent(
                    EventType.TYPE_NAME_COMPATIBILITY_CONTINUE,
                    null,
                    null
                )
            }
        }
    }

    override fun onResume() {
        super.onResume()
        buttonIsEnable()
    }

    private fun buttonIsEnable() {
        if (edit_text_name_1.text.isEmpty() || edit_text_name_2.text.isEmpty()) {

            text_view_get_result.isClickable = false
            text_view_get_result.alpha = 0.5f

        } else {
            text_view_get_result.isClickable = true
            text_view_get_result.alpha = 1f
        }
    }

}
